// 获取所有的可折叠文件夹图标
document.querySelectorAll('.toggle').forEach(toggle => {
    toggle.addEventListener('click', function() {
        const parentLi = this.parentElement.parentElement;
        const childUl = parentLi.querySelector('ul');
        if (childUl) {
            if (childUl.style.display === 'none' || childUl.style.display === '') {
                childUl.style.display = 'block';
                this.textContent = '[-]';
            } else {
                childUl.style.display = 'none';
                this.textContent = '[+]';
            }
        }
    });
});

// 点击文件夹名称展开/折叠目录
document.querySelectorAll('.folder-name').forEach(folderName => {
    folderName.addEventListener('click', function() {
        const parentLi = this.closest('li');
        const childUl = parentLi.querySelector('ul');
        const toggle = parentLi.querySelector('.toggle');
        if (childUl) {
            if (childUl.style.display === 'none' || childUl.style.display === '') {
                childUl.style.display = 'block';
                toggle.textContent = '[-]';
            } else {
                childUl.style.display = 'none';
                toggle.textContent = '[+]';
            }
        }
    });
});

// 获取所有备注输入框并添加事件监听
document.querySelectorAll('.note-input').forEach(input => {
    input.addEventListener('blur', function() {
        const form = this.closest('.note-form');
        const path = form.dataset.path;
        const note = this.value;

        // 使用 Fetch API 实时保存备注
        fetch('save_note.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ filename: path, note: note })
        })
        .then(response => response.json())
        .then(data => {
            console.log('保存成功:', data);
        })
        .catch(error => {
            console.error('保存失败:', error);
        });
    });
});
